<template>
  <div>
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="产品名称">
          <el-input v-model="formData.name" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品品牌">
          <el-input v-model="formData.brand" placeholder="请输入产品品牌" />
        </el-form-item>
        <el-form-item label="产品类别">
          <el-select v-model="formData.model" placeholder="请选择产品类别" clearable>
            <el-option
              v-for="item in modelclass"
              :key="item.id"
              :label="item.info"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商">
          <el-input v-model="formData.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getdata">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </el-form-item>
      </el-form>

    </div>

    <div>
      <el-table :data="tableData" border>
        <el-table-column prop="product.id" label="产品编码" />
        <el-table-column prop="product.name" label="产品名字" />
        <el-table-column prop="product.brand" label="产品品牌" />
        <el-table-column prop="product.model.info" label="产品类别" />
        <el-table-column prop="supplyPrice" label="零售价(元)" />
        <el-table-column prop="suggestedPrice" label="建议价(元)" />

        <el-table-column prop="supplier.name" label="供应商">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>供应商名字: {{ scope.row.supplier.name }}</p>
              <p>供应商地址: {{ scope.row.supplier.address }}</p>
              <p>供应商负责人: {{ scope.row.supplier.contact }}</p>
              <p>供应商负责人电话: {{ scope.row.supplier.phone }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag type="info">{{ scope.row.supplier.name }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>存货数量: {{ scope.row.quantity }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag v-if="scope.row.status==0" effect="dark" type="success">有货</el-tag>
                <el-tag v-if="scope.row.status==1" effect="dark" type="info">缺货</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="danger" @click="remove(scope.row)">删除</el-button>
            <el-button size="small" type="danger" @click="update(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getPagedata"
      />
    </div>

    <el-dialog
      title="添加商品种类"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >

      <el-form ref="productInfo" :model="productInfo" label-suffix=":" label-position="right" label-width="120px" :rules="rules">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="productInfo.name" placeholder="请输入产品名称" style="width: 300px" />
        </el-form-item>
        <el-form-item label="产品品牌" prop="brand">
          <el-input v-model="productInfo.brand" placeholder="请输入产品品牌" style="width: 300px" />
        </el-form-item>

        <el-form-item label="产品类型" prop="model">
          <el-select v-model="productInfo.model" placeholder="请选择产品类型" style="width: 300px">
            <el-option
              v-for="item in modelclass"
              :key="item.id"
              :label="item.info"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="零售价(元)" prop="supplyPrice">
          <el-input v-model="productInfo.supplyPrice" placeholder="请输入零售价" style="width: 300px" />
        </el-form-item>
        <el-form-item label="建议销售价(元)" prop="suggestedPrice">
          <el-input v-model="productInfo.suggestedPrice" placeholder="请输入建议价" style="width: 300px" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="productInfo.quantity" placeholder="请输入产品数量" style="width: 300px" />
        </el-form-item>
        <el-form-item label="供应商" prop="model">
          <el-select v-model="productInfo.supplier" placeholder="请选择供应商" style="width: 300px">
            <el-option
              v-for="item in allsupplier"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addproduct('productInfo')">添加</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="修改"
      :visible.sync="updateVisible"
      width="35%"
      :before-close="handleupdateClose"
    >

      <el-form ref="updateproductInfo" :model="updateproductInfo" label-suffix=":" label-position="right" label-width="120px" :rules="urules">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="updateproductInfo.name" placeholder="请输入产品名称" :disabled="true" />
        </el-form-item>
        <el-form-item label="零售价(元)" prop="supplyPrice">
          <el-input v-model.number="updateproductInfo.supplyPrice" />
        </el-form-item>
        <el-form-item label="建议销售价(元)" prop="suggestedPrice">
          <el-input v-model="updateproductInfo.suggestedPrice" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="updateproductInfo.status"
            inactive-color="#13ce66"
            active-color="#ff4949"
            active-text="缺货"
            inactive-text="有货"
          />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="updateproductInfo.quantity" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleupdate('updateproductInfo')">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import productApi from '@/api/product/productset'
import supplierApi from '@/api/supplier/supplier'
export default {
  data() {
    var valiNumberPass2 = (rule, value, callback) => {
      const reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g
      if (value === '') {
        callback(new Error('亲，这里不能为空哦！'))
      } else if (!reg.test(value)) {
        callback(new Error('亲，这里必须为数字值'))
      } else {
        callback()
      }
    }

    return {
      formData: {

      },
      tableData: null,
      current: 1, // 当前页
      limit: 10, // 每页显示记录数
      total: 0, // 总记录数
      dialogVisible: false,
      productInfo: {
        name: '',
        brand: '',
        model: '',
        supplier: ''
      },
      rules: {
        name: [
          { required: true, message: '亲，这里不能为空哦！', trigger: 'blur' }
        ],
        brand: [
          { required: true, message: '亲，这里不能为空哦！', trigger: 'blur' }
        ],
        model: [
          { required: true, message: '亲，这里不能为空哦！', trigger: 'change' }
        ],
        supplier: [
          { required: true, message: '亲，这里不能为空哦！', trigger: 'change' }
        ],
        supplyPrice: [
          { required: true, validator: valiNumberPass2, trigger: 'blur' }
        ],
        suggestedPrice: [
          { required: true, validator: valiNumberPass2, trigger: 'blur' }
        ],
        quantity: [
          { required: true, validator: valiNumberPass2, trigger: 'blur' }
        ]

      },
      modelclass: null,
      allsupplier: null,
      updateVisible: false,
      updateproductInfo: {
        productId: '',
        supplierId: '',
        name: '',
        supplyPrice: '',
        suggestedPrice: '',
        status: '',
        quantity: '',
        ishave: ''
      },
      urules: {
        supplyPrice: [
          { required: true, message: '亲，这里不能为空哦！', trigger: 'blur' }
        ],
        suggestedPrice: [
          { required: true, message: '亲，这里不能为空哦！', trigger: 'blur' }
        ],
        quantity: [
          { required: true, message: '亲，这里不能为空哦！', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getdata()
    this.getclass()
    this.getsupplier()
  },
  methods: {
    getclass() {
      productApi.getclass().then(res => {
        this.modelclass = res.data
      })
    },
    getsupplier() {
      supplierApi.getAllsupplier().then(res => {
        this.allsupplier = res.data
      })
    },
    remove(row) {
      this.$confirm('此操作将永久删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 确定执行then方法
        // 调用接口
        // 删除
        supplierApi.remove(row.product.id, row.supplier.id).then(res => {
          if (res.code === 500) {
            this.$message({
              type: 'warning',
              message: res.msg
            })
          }
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            // 刷新页面
            this.getdata()
          }
        })
      })
    },
    // 获取数据
    getdata() {
      this.formData.page = this.current
      this.formData.limit = this.limit
      supplierApi.getAllProduct(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    getPagedata(page = 1) {
      this.current = page
      this.formData.page = this.current
      this.formData.limit = this.limit
      supplierApi.getAllProduct(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    addproduct(productInfo) {
      // eslint-disable-next-line no-undef
      this.$refs[productInfo].validate((valid) => {
        if (valid) {
          supplierApi.addproduct(this.productInfo).then(res => {
            if (res.code === 500) {
              this.$message({
                type: 'warning',
                message: res.msg
              })
            }
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.dialogVisible = false
              this.productInfo = {}
              // 刷新页面
              this.getdata()
            }
          })
          // 添加商品
          console.log(this.productInfo)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    },
    update(row) {
      this.updateVisible = true
      this.updateproductInfo.name = row.product.name
      this.updateproductInfo.productId = row.product.id
      this.updateproductInfo.supplier = row.supplier.name
      this.updateproductInfo.supplierId = row.supplier.id
      this.updateproductInfo.supplyPrice = row.supplyPrice
      this.updateproductInfo.suggestedPrice = row.suggestedPrice
      this.updateproductInfo.status = (row.status !== 0)
      this.updateproductInfo.quantity = row.quantity
    },
    handleupdate(updateproductInfo) {
      this.$refs[updateproductInfo].validate((valid) => {
        if (valid) {
          this.updateproductInfo.status = this.updateproductInfo.status === true ? 1 : 0
          console.log(this.updateproductInfo)
          // 修改商品
          supplierApi.update(this.updateproductInfo).then(res => {
            if (res.code === 500) {
              this.$message({
                type: 'warning',
                message: res.msg
              })
            }
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.updateVisible = false
              // 刷新页面
              this.getdata()
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleupdateClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped lang="scss">

</style>

